/*
 * histSecuencias.java
 *
 * Created on 10 de septiembre de 2008, 10:10 AM
 */

package administrador;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;
/**
 *
 * @author  daniel1
 */
public class histSecuencias extends javax.swing.JFrame {
Usuario user;
int bodega;
Conexion conexion;

JTable tabla;
 JScrollPane js;
    /** Creates new form histSecuencias */
    public histSecuencias(Conexion conexion,int bodega,Usuario user) {
        this.user=user;
        this.bodega=bodega;
        this.conexion=conexion;
        initComponents();
        refrescar();
        setVisible(true);
        setBounds(100,100,380,430);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cb_tipo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        lbl_num = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(histSecuencias.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        cb_tipo.setName("cb_tipo"); // NOI18N
        cb_tipo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cb_tipoItemStateChanged(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        lbl_num.setFont(resourceMap.getFont("lbl_num.font")); // NOI18N
        lbl_num.setForeground(resourceMap.getColor("lbl_num.foreground")); // NOI18N
        lbl_num.setText(resourceMap.getString("lbl_num.text")); // NOI18N
        lbl_num.setName("lbl_num"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cb_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbl_num, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cb_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbl_num, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                            .addComponent(jLabel3))))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(13, 13, 13)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cb_tipoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cb_tipoItemStateChanged
// TODO add your handling code here:
    int seleccion=cb_tipo.getSelectedIndex();
    String comp=cb_tipo.getItemAt(seleccion).toString();
    String consulta="";
    if(comp.equals("Factura")){
    consulta="SELECT max(cod_pedido) FROM pedido p where tipo_comprobante like 'F';";
    }else if(comp.equals("Nota de Venta")){
    consulta="SELECT max(cod_pedido) FROM pedido p where tipo_comprobante like 'N';";
    }else if(comp.equals("Nota de Credito")){
    consulta="SELECT MAX(cod_nota) FROM nota n where tipo_Comprobante like 'C';";
    }else if(comp.equals("Nota de Debito")){
    consulta="SELECT MAX(cod_nota) FROM nota n where tipo_Comprobante like 'D';";
    }else if(comp.equals("Factura de Compras")){
    consulta="SELECT MAX(cod_kfactura) FROM kfactura n where tipo_Comprobante like 'E';";
    }else if(comp.equals("Devolucion en Compras")){
    consulta="SELECT MAX(cod_kfactura) FROM kfactura n where tipo_Comprobante like 'S';";
    }
    ResultSet resp=conexion.consultarBase(consulta);
    int numero=0;
    try {
        resp.beforeFirst();
        resp.next();
        numero=resp.getInt(1);
        
    } catch (SQLException ex) {
        Logger.getLogger(histSecuencias.class.getName()).log(Level.SEVERE, null, ex);
    }
    lbl_num.setText(String.valueOf(numero));
    
}//GEN-LAST:event_cb_tipoItemStateChanged

    public void refrescar(){
        
        ResultSet respuesta;
        respuesta=conexion.consultarBase("SELECT fecha,nombre,num_secuencia,fecha_fin,numero FROM tipo_comprobante t,autorizacion a where t.cod_autorizacion=a.cod_auto order by fecha;");
        
        //Obtengo Filas-Arreglo de Titulos de Columnas
        int filas=conexion.obtenerFilas(respuesta);
        String[] titulosCol=conexion.obtenerColumnas(respuesta);
        
        //Creo la tabla:
        
        CrearTabla(filas,titulosCol);
        llenaTabla(respuesta,titulosCol.length,filas);
       
       //lleno el Combo de Comprobantes:
        llenoComprobante();
    }
    
    
    public void llenoComprobante() {
        try {
            ResultSet resp = conexion.consultarBase("SELECT nombre FROM tipo_comprobante group by nombre;");
            resp.beforeFirst();
            while (resp.next()) {
                String etiqueta = resp.getString(1);
                this.cb_tipo.addItem(etiqueta);
            }
        } catch (SQLException ex) {
            Logger.getLogger(histSecuencias.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    public void llenaTabla(ResultSet r,int columnas,int filas){
        try{
            r.beforeFirst();
            
            int j=0;
            while(r.next()) {
                
                
                for(int i=0;i<columnas;i++) {
                    
                    Object toma=r.getObject(i+1);
                    //System.out.println("toma valor:"+toma+" * fila:"+j+" * column:"+i);
                    try{
                        tabla.setValueAt(toma,j,i);
                    }catch(java.lang.ArrayStoreException e){
                        tabla.setValueAt("null",j,i);
                    }
                }
                
            j++;    
            }
        }catch(SQLException sqle){
           //System.out.println("Error al mostrar los datos");
            System.err.println(sqle);
        }
        
    }
    
public void CrearTabla(int filas,String[] titulos){
        
        int columnas=titulos.length;
        String[][] tabla0=new String[filas][columnas];
       
        tabla= new JTable(tabla0,titulos);
        
        //Aqui le especifico para que la tabla acepte valores doubles e int:
        //--------------------------------------------------------------------
        final boolean[] canEdit = new boolean [columnas];
        for(int p=0;p<columnas;p++){
        canEdit[p]=false;
        }
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            tabla0,titulos  
        ) {
           // boolean[] canEdit = new boolean [] { false, false, false, false};

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        //---------------------------------------------------------------------
         //Le doy el evento del LCick del Mouse:
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        //remuevo el JScroll para que no se monte cuando cree el nuevo:
        //*************************************************************
        try{
        jPanel1.remove(js);
        }catch(java.lang.NullPointerException e){}
        //*************************************************************
        
        //Pongo en la tabla:
            js = new JScrollPane(tabla);
                        
            js.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(198, 196, 196)));
            js.setBounds(22, 100, 380, 130);
            js.setVisible(true);
            jPanel1.add(js);
            js.updateUI();
            jPanel1.updateUI();
    }
 

 private void tablaMouseClicked(java.awt.event.MouseEvent evt) {
    // Evento del click del mouse sobre la tabla:
     
 }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_tipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField lbl_num;
    // End of variables declaration//GEN-END:variables

}
